package com.material.c2c.web.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cpt.framework.web.JsonResult;
import com.material.c2c.user.entity.ADMIN;
import com.material.c2c.user.service.AdminService;
import com.material.c2c.web.user.model.LoginVO;
import com.material.c2c.web.user.model.QueryVO;
import com.material.c2c.web.util.ControllerUtil;

@RestController
@RequestMapping("/admin")
public class AdminController {
	@Resource
	AdminService adminService;
	@Autowired
    private HttpServletRequest request;
	@PostMapping("/search")
	public JsonResult search(@RequestBody QueryVO vo) {
	  // 分页查询管理员信息
	  return JsonResult.success(adminService.search(vo.getKey(), vo.getPageable()));
	}
	@PostMapping("/add")
	public JsonResult add(@RequestBody ADMIN admin) {
		return ControllerUtil.getResult(adminService.add(admin));
	}
	@PostMapping("/update")
	public JsonResult update(@RequestBody ADMIN admin) {
		return ControllerUtil.getResult(adminService.update(admin));
	}
	@GetMapping("/delete/{id}")
	public JsonResult delete(@PathVariable String id) {
		return ControllerUtil.getResult(adminService.delete(id));
	}
	@GetMapping("/getOne/{id}")
	public JsonResult getOne(@PathVariable String id) {
		return JsonResult.success(adminService.getOne(id));
	}
	@PostMapping("/login")
    public JsonResult login(@RequestBody LoginVO vo,HttpServletResponse response) {
      if(vo.getUserName() == null || "".equals(vo.getUserName())) {
        return JsonResult.failed("输入账号有误！");
      } 
      if(vo.getPassword() == null || "".equals(vo.getPassword())) {
        return JsonResult.failed("输入密码有误！");
      }
      ADMIN user = adminService.getByGlyxm(vo.getUserName());
      if(user == null) {
        return JsonResult.failed("账号不存在！");
      }
      if(!vo.getPassword().equals(user.getGlymm())) {
        return JsonResult.failed("密码错误！");
      }
      HttpSession session = request.getSession();
      session.setAttribute("admin", user);
      Cookie cookie = new Cookie("adminId",user.getId());
      response.addCookie(cookie);
      return JsonResult.success(user);
    }
    @GetMapping("/logout")
    public JsonResult logout(HttpServletResponse response) {
      // 删除cookie
      removeCookie(response,"adminId");
      // 删除session
      HttpSession session = request.getSession();
      session.removeAttribute("admin");
      return JsonResult.SUCCEED;
    }
    /**
     * 移除cookie
     * @param response
     * @param name
     * @since 1.0
     * @version 1.0
     * @author QinYoubin (qin.youbin@sccaptain.com.cn)
     */
    private void removeCookie(HttpServletResponse response,String name) {
      Cookie[] cookies = request.getCookies();
      if (null==cookies) {
          return;
      } else {
          for(Cookie cookie : cookies){
              // 如果找到同名cookie，就将value设置为null，将存活时间设置为0，再替换掉原cookie，这样就相当于删除了。
              if(cookie.getName().equals(name)) {
                  cookie.setValue(null);
                  cookie.setMaxAge(0);
                  cookie.setPath("/");
                  response.addCookie(cookie);
                  break;
              }
          }
      }
    }
}
